package cn.laixueit.service.impl;

import cn.laixueit.common.CodecUtils;
import cn.laixueit.common.Utils;
import cn.laixueit.mapper.UserMapper;
import cn.laixueit.pojo.User;
import cn.laixueit.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.util.DecodeUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Boolean check(String data, Integer type) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        switch (type){
            case 1:
                wrapper.eq("username",data);
                break;
            case 2:
                wrapper.eq("phone",data);
                break;
            default:
                return  null;
        }
        int count = count(wrapper);
        return count == 0;
    }

    @Override
    public void code(String phone) {
        HashMap<String, String> map = new HashMap<>();
        map.put("phone",phone);
        String code = Utils.randCode();
        map.put("code",code);
        amqpTemplate.convertAndSend("","leyou_sms",map);
        stringRedisTemplate.boundValueOps("sms:"+phone+":code").set(code);
        stringRedisTemplate.boundValueOps("sms:"+phone+":code").expire(5, TimeUnit.MINUTES);
    }



    @Override
    public void register(User user, String code) {
        String phone = user.getPhone();
        if(StringUtils.isNotBlank(phone)){
            String redisCode = stringRedisTemplate.boundValueOps("sms:" + phone + ":code").get();
            if(!StringUtils.equals(redisCode,code)){
                return;
            }
        }

        String password = user.getPassword();

        user.setCreated(new Date());

        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);

        String md5Password = CodecUtils.md5Hex(password, salt);
        user.setPassword(md5Password);

        save(user);

        //stringRedisTemplate.delete("sms:"+phone+":code");

    }

    @Override
    public User query(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = getOne(wrapper);
        if(user!=null){
            String salt = user.getSalt();
            String md5Password = CodecUtils.md5Hex(password, salt);
            if(StringUtils.equals(md5Password,user.getPassword())){
                return user;
            }
        }
        return null;
    }
}
